package com.cookor.poi.modules.phy.service;

import com.cookor.poi.common.persistence.DataEntity;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.service.CrudService;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.phy.dao.TEvaluateDao;
import com.cookor.poi.modules.phy.entity.TEvaluate;
import com.cookor.poi.modules.phy.entity.TOrder;
import com.cookor.poi.modules.phy.entity.TWeixinUser;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class TEvaluateService extends CrudService<TEvaluateDao, TEvaluate> {
  @Autowired
  private TOrderService tOrderService;
  
  @Autowired
  private TWeixinUserService tWeixinUserService;
  
  public TEvaluate get(String id) {
    return (TEvaluate)super.get(id);
  }
  
  public List<TEvaluate> findList(TEvaluate tEvaluate) {
    return super.findList( tEvaluate);
  }
  
  public Page<TEvaluate> findPage(Page<TEvaluate> page, TEvaluate tEvaluate) {
    return super.findPage(page,  tEvaluate);
  }
  
  @Transactional(readOnly = false)
  public void save(TEvaluate tEvaluate) {
    super.save( tEvaluate);
  }
  
  @Transactional(readOnly = false)
  public void delete(TEvaluate tEvaluate) {
    super.delete( tEvaluate);
  }
  
  @Transactional(readOnly = false, rollbackFor = {Exception.class})
  public void add(Integer tenantId, Integer orderId, String openId, Integer star, String content, Integer noShow) throws Exception {
    if (tenantId == null || orderId == null || StringUtils.isBlank(openId) || star == null || noShow == null)
      throw new Exception("参数错误"); 
    TOrder tOrder = this.tOrderService.get(orderId.toString());
    TWeixinUser user = this.tWeixinUserService.getByOpenId(openId);
    TEvaluate tEvaluate = new TEvaluate();
    tEvaluate.setTenantId(tenantId);
    tEvaluate.setOrderId(orderId);
    tEvaluate.setPatientId(tOrder.getPatientId());
    tEvaluate.setDoctorId(tOrder.getDoctorId());
    tEvaluate.setRankId(tOrder.getRankId());
    tEvaluate.setRankName(tOrder.getRankName());
    tEvaluate.setPatientName(tOrder.getPatientName());
    tEvaluate.setDoctorName(tOrder.getDoctorName());
    tEvaluate.setStar(star);
    tEvaluate.setContent(content);
    tEvaluate.setOpenId(openId);
    tEvaluate.setPic(user.getHeadimgurl());
    tEvaluate.setNickName(user.getNickname());
    tEvaluate.setNoShow(noShow);
    save(tEvaluate);
    tOrder.setStatus(Integer.valueOf(10));
    this.tOrderService.save(tOrder);
  }
}
